<?php

class TagLibArticle extends TagLib {

        /**
         * 标签定义：attr 属性列表close 是否闭合（0 或者1 默认1）alias 标签别名level 嵌套层次 
         * @var type 
         */
        protected $tags = array(
            'artlist' => array('attr' => 'module,table,sql,order,page,pagesize,limit,key,id,mod,join', 'level' => 3),
        );

        // join 例:array(' work ON artist.id = work.artist_id','card ON artist.card_id = card.id')

        public function _artlist($attr, $content) {
                //申明静态变量 缓存数据
                static $_iterateParseCache = array();
                $cacheIterateId = md5($attr . $content);
                if ($_iterateParseCache[$cacheIterateId])
                        return $_iterateParseCache[$cacheIterateId];
                $tag = $this->parseXmlAttr($attr, 'artlist');
                //参数格式化

                if (!empty($tag['sql'])) {
                        //如果 检测到有SQL输入.则直接支持SQL
                        $sql = str_replace("@#_", C("DB_PREFIX"), $tag['sql']);
                        $model = new Model();
                        $list = $model->query($sql);
                } else {
                        //判断是否多表联合查询
                        if (!empty($tag['join'])) {
                                //必需要以数组格式传入 
                                eval("\$join=" . $tag['join'] . ";");
                        }
                        $join = $join ? $join : NULL;
                        //非SQL
                        $tag['module'] = $tag['module'] ? $tag['module'] : MODULE_NAME;

                        if ($tag['table']) {
                                $_tempModule = new Model();
                                $tables = $tag['table'] ? $tag['table'] : NULL;
                            
                        } else {
                                $_tempModule = D($tag['module']);
                        }

                        //条件参数  有两种方式传入 1.POST传入2.标签传入
                        $where = array();
                        //标签传入 解析
                        if (!empty($tag['where'])) {
                                $_tempWhere = str_replace("^", ">", $tag['where']);
                                if (FALSE !== strpos($tag['where'], "array")) {
                                        eval("\$tagwhere_a=" . $_tempWhere . ";");
                                } else {
                                        $tagwhere_a = str_replace("@#_", C("DB_PREFIX"), $_tempWhere);
                                }
                        }
                        //POST传入 解析
                        if (isset($_POST['where'])) {
                                $_tableFields = $_tempModule->getDbFields();
                                foreach ($_POST['where'] as $key => $value) {

                                        if (in_array($key, $_tableFields)) {
                                                if (FALSE !== strpos($value, ",")) {
                                                        list($exp, $r) = explode(",", $value);
                                                        $tagwhere_b[$key] = array($exp, $r);
                                                } else {
                                                        if (in_array($key, explode("|", C("DB_LIKE_FIELDS")))) {
                                                                $tagwhere_b[$key] = array("like", "%" . $value . "%");
                                                        } else {
                                                                $tagwhere_b[$key] = array("eq", $value);
                                                        }
                                                }
                                        }
                                }
                        }
                        //条件重组 所有POST传入数据.覆盖标签传入数据,标签传入数据可作默认数据
                        $condition = array_merge(is_array($tagwhere_a) ? $tagwhere_a : (array) $tagwhere_a, is_array($tagwhere_b) ? $tagwhere_b : (array) $tagwhere_b);
                        //排序
                        $order = empty($tag['order']) ? NULL : $tag['order'];
                        //字段
                        $field = empty($tag['field']) ? implode(",", $_tableFields) : $tag['field'];
                        //是否分页
                        if ($tag['page'] == "true") {
                                //如果分页 则先查询出所有数据条数据
                                $count = $_tempModule->where($condition)->table($tables)->join($join)->count($_tempModule->getPk());
                                if ($count > 0) {
                                        $pagesize = $tag['pagesize'] ? (int) $tag['pagesize'] : 20;
                                        $Page = new Page($count, $pagesize);
                                        $list = $_tempModule->where($condition)
                                                ->table($tables)
                                                ->order($order)
                                                ->limit($Page->firstRow . ',' . $Page->listRows)
                                                ->field($field)
                                                ->join($join)
                                                ->select();
                                        $this->tpl->set("page" . $cacheIterateId, $Page->show());
                                }
                        } else {
                                $limit = !empty($tag['limit']) ? intval($tag['limit']) : 20;
                                $list = $_tempModule->where($condition)
                                        ->table($tables)
                                        ->order($order)
                                        ->limit($limit)
                                        ->field($field)
                                        ->join($join)
                                        ->select();
                        }
                }
          

                // dump($_tempModule->getLastSql());exit;
                // 模板输出
                $key = !empty($tag['key']) ? $tag['key'] : "key";
                $id = !empty($tag['id']) ? $tag['id'] : "list";
                $mod = !empty($tag['mod']) ? intval($tag['mod']) : 2;
                $this->tpl->set($cacheIterateId, $list);
                $parsestr .= '<?php $page = $this->get("page' . $cacheIterateId . '");$_resule = $this->get(\'' . $cacheIterateId . '\');if ($_resule): $' . $key . '=0;';
                $parsestr .= 'foreach($_resule as $key=>$' . $id . '):';
                $parsestr .= '++$' . $key . ';$mod = ($' . $key . ' % ' . $mod . ' );?>';
                $parsestr .= $content . '<?php endforeach; endif;?>';
                $_iterateParseCache[$cacheIterateId] = $parsestr;
                return $parsestr;
        }

}

// end class
